package org.jeecg.modules.demo.wz.service.impl;

import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.demo.wz.entity.Serviceorder;
import org.jeecg.modules.demo.wz.entity.Servicestate;
import org.jeecg.modules.demo.wz.entity.Servicetask;
import org.jeecg.modules.demo.wz.entity.Servicetissue;
import org.jeecg.modules.demo.wz.mapper.ServiceorderMapper;
import org.jeecg.modules.demo.wz.service.IServiceorderService;
import org.jeecg.modules.demo.wz.service.IServicestateService;
import org.jeecg.modules.demo.wz.service.IServicetaskService;
import org.jeecg.modules.demo.wz.service.IServicetissueService;
import org.jeecg.modules.demo.wz.utils.CommonResult;
import org.jeecg.modules.demo.wz.vo.ServiceOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 服务订单表
 * @Author: jeecg-boot
 * @Date:   2022-03-29
 * @Version: V1.0
 */
@Service
public class ServiceorderServiceImpl extends ServiceImpl<ServiceorderMapper, Serviceorder> implements IServiceorderService {

    @Autowired
    private IServicestateService iServicestateService;
    @Autowired
    private IServicetaskService iServicetaskService;
    @Autowired
    private IServicetissueService iServicetissueService;



    @Override
    public CommonResult orderList() {
        List<Serviceorder> orderList = list();
        List<ServiceOrderVo> serviceOrderVos=new ArrayList<>();
        if(orderList!=null){
            for (Serviceorder serviceorder : orderList) {
                ServiceOrderVo serviceOrderVo=new ServiceOrderVo();
                serviceOrderVo.setId(serviceorder.getId());
                Date time = serviceorder.getTime();
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                String date = sdf.format(time);
                serviceOrderVo.setTime(date);
                serviceOrderVo.setName(serviceorder.getName());
                Servicestate state = iServicestateService.getById(serviceorder.getStateid());
                Servicetask task = iServicetaskService.getById(serviceorder.getTaskid());
                Servicetissue tissue = iServicetissueService.getById(serviceorder.getTissueid());
                if (state!=null){
                    serviceOrderVo.setState(state.getStatename());
                }else {
                    new RuntimeException("无相关数据");
                }
                if (task!=null){
                    serviceOrderVo.setTask(task.getTaskname());
                }else {
                    new RuntimeException("无相关数据");
                }
                if (tissue!=null){
                    serviceOrderVo.setTissue(tissue.getTissuename());
                }else {
                    new RuntimeException("无相关数据");
                }
                serviceOrderVos.add(serviceOrderVo);
            }
            return new CommonResult(200,"查询成功",serviceOrderVos);
        }else {
            return new CommonResult(400,"无相关数据");
        }
    }


    @Override
    public CommonResult orderCount() {
        int orderCount = count();
        if (orderCount>=0){
            return new CommonResult(200,"查询成功",orderCount);
        }
        return new CommonResult(400,"无相关数据");
    }


}
